home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / grocery.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-17  |  11.5 KB  |  440 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: grocery.cpp 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 09/18/1997
  9. // Date Last Modified: 03/18/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. This is a test program used to test the (P)ersistent base class
  32. in a practical database application.
  33. */
  34. // ----------------------------------------------------------- // 
  35. #include "grocery.h"
  36. #include "strutil.h"
  37.  
  38. Grocery::Grocery(POD *pod) : Persistent(pod)
  39. {
  40.   name = "\0";
  41.   brand = "\0";
  42.   store = "\0";
  43.   price = 0;
  44.   quantity = 0;
  45.   purchasing = 'N';
  46.   line_total = 0;
  47. }
  48.  
  49. Grocery::Grocery(const POD *pod) : Persistent(pod)
  50. {
  51.   name = "\0";
  52.   brand = "\0";
  53.   store = "\0";
  54.   price = 0;
  55.   quantity = 0;
  56.   purchasing = 'N';
  57.   line_total = 0;
  58. }
  59.  
  60. Grocery::Grocery() 
  61. {
  62.   name = "\0";
  63.   brand = "\0";
  64.   store = "\0";
  65.   price = 0;
  66.   quantity = 0;
  67.   purchasing = 'N';
  68.   line_total = 0;
  69. }
  70.  
  71. unsigned Grocery::ObjectLength()
  72. {
  73.   unsigned len = StringFileLength(name) + \
  74.     StringFileLength(brand) + \
  75.     StringFileLength(store) + \
  76.     sizeof(price)    + \
  77.     sizeof(quantity) + \
  78.     sizeof(purchasing) + \
  79.     sizeof(line_total);
  80.   
  81.   return len;
  82. }
  83.  
  84. FAU Grocery::Write()
  85. {
  86.   ObjectHeader oh;
  87.  
  88.   FAU addr = pod->OpenDatabase()->Alloc(ObjectLength() + sizeof(ObjectHeader));
  89.   if(!addr) return 0;
  90.   
  91.   oh.ClassID = GetClassID();
  92.   oh.ObjectID = addr;
  93.   WriteObjHeader(oh);
  94.  
  95.   WriteString(name);
  96.   WriteString(brand);
  97.   WriteString(store);
  98.   pod->OpenDatabase()->Write(&price, sizeof(price));
  99.   pod->OpenDatabase()->Write(&quantity, sizeof(quantity));
  100.   pod->OpenDatabase()->Write(&purchasing, sizeof(purchasing));
  101.   pod->OpenDatabase()->Write(&line_total, sizeof(line_total));
  102.   objectaddress = addr;
  103.   
  104.   // Add the entry to the Index file
  105.   if (UsingIndex()) {
  106.     EntryKey key(name.c_str(), oh.ObjectID, oh.ClassID);
  107.     AddKey(key);
  108.   }
  109.  
  110.   return addr;
  111. }
  112.  
  113. void Grocery::Read(FAU Address)
  114. {
  115.   ObjectHeader oh;
  116.  
  117.   ReadObjHeader(oh, Address);
  118.   if(oh.ClassID != GetClassID()) return; // Incorrect object type
  119.  
  120.   ReadString(name);
  121.   ReadString(brand);
  122.   ReadString(store);
  123.   pod->OpenDatabase()->Read(&price, sizeof(price));
  124.   pod->OpenDatabase()->Read(&quantity, sizeof(quantity));
  125.   pod->OpenDatabase()->Read(&purchasing, sizeof(purchasing));
  126.   pod->OpenDatabase()->Read(&line_total, sizeof(line_total));
  127.   objectaddress = Address;
  128. }
  129.  
  130. FAU Grocery::Find()
  131. {
  132.   Grocery grocery;
  133.   
  134.   grocery.name = this->name;
  135.   grocery.brand = this->brand;
  136.   grocery.store = this->store;
  137.   grocery.price = this->price;
  138.   grocery.quantity = this->quantity;
  139.   grocery.purchasing = this->purchasing;
  140.   grocery.line_total = this->line_total;
  141.  
  142.   int rv; // Return value
  143.  
  144.   // Search the index file for this entry
  145.   if(UsingIndex()) {
  146.     EntryKey e(this->name.c_str());
  147.     rv = FindKey(e);
  148.     if(rv) { // Found the object in the index file
  149.       Read(e.object_address);
  150.       return e.object_address;
  151.     }
  152.     else
  153.       return 0;
  154.   }
  155.  
  156.   FAU addr = 0;
  157.   ObjectHeader oh;
  158.  
  159.   while(1)
  160.     {
  161.       addr = pod->OpenDatabase()->FindFirstObject(addr);
  162.       if(!addr) break;
  163.       ReadObjHeader(oh, addr);
  164.       if(oh.ClassID == GetClassID()) {
  165.     Read(addr);
  166.         rv = CaseICmp(name, grocery.name);
  167.     if(rv == 0) {
  168.       objectaddress = addr;
  169.       return addr; // Found unique data member
  170.     }
  171.     else {
  172.       // Reset the objects data
  173.           this->name = grocery.name;
  174.       this->brand = grocery.brand;
  175.       this->store = grocery.store;
  176.       this->price = grocery.price;
  177.       this->quantity = grocery.quantity;
  178.           this->purchasing = grocery.purchasing;
  179.           this->line_total = grocery.line_total;
  180.     }
  181.       }
  182.     }
  183.   return 0; // Could not find 
  184. }
  185.  
  186. void Grocery::Copy(const Grocery &ob)
  187. {
  188.   name = ob.name;
  189.   brand = ob.brand;
  190.   store = ob.store;
  191.   price = ob.price;
  192.   quantity = ob.quantity;
  193.   price = ob.price;
  194.   purchasing = ob.purchasing;
  195.   line_total = ob.line_total;
  196. }
  197.  
  198. int Grocery::FullCompare(const Grocery &ob)
  199. {
  200.   if(ob.name != name) return 0;
  201.   if(ob.brand != brand) return 0;
  202.   if(ob.store != store) return 0;
  203.   if(ob.price != price) return 0;
  204.   if(ob.quantity != quantity) return 0;
  205.   if(ob.purchasing != purchasing) return 0;
  206.   if(ob.line_total != line_total) return 0;
  207.   return 1;
  208. }
  209.  
  210. int operator==(const Grocery &a, const Grocery &b)
  211. {
  212.   return CaseICmp(a.GetName(), b.GetName()) == 0;
  213. }
  214.  
  215. int operator!=(const Grocery &a, const Grocery &b)
  216. {
  217.   return CaseICmp(a.GetName(), b.GetName()) != 0;
  218. }
  219.  
  220. int operator>(const Grocery &a, const Grocery &b)
  221. {
  222.   return CaseICmp(a.GetName(), b.GetName()) > 0;
  223. }
  224.  
  225. int operator>=(const Grocery &a, const Grocery &b)
  226. {
  227.   return CaseICmp(a.GetName(), b.GetName()) >= 0;
  228. }
  229.   
  230. int operator<(const Grocery &a, const Grocery &b)
  231. {
  232.   return CaseICmp(a.GetName(), b.GetName()) < 0;
  233. }
  234.   
  235. int operator<=(const Grocery &a, const Grocery &b)
  236. {
  237.   return CaseICmp(a.GetName(), b.GetName()) <= 0;
  238. }
  239.  
  240. FAU Grocery::Delete()
  241. {
  242.   if(UsingIndex()) {
  243.     EntryKey e(this->name.c_str());
  244.     int rv = FindKey(e);
  245.     if(rv) {  // Found the object in the index file
  246.       FAU addr = e.object_address;
  247.       DeleteObject(e.object_address); // Delete from the data file
  248.       RemoveKey(e);                   // Remove from the index file
  249.       return addr;
  250.     }
  251.     else
  252.       return 0; // Could not delete
  253.   }
  254.  
  255.   FAU addr = Find();
  256.   if(!addr) return 0; // Object does not exist
  257.   DeleteObject(addr);
  258.   return addr;
  259. }
  260.  
  261. FAU Grocery::Remove()
  262. {
  263.   if(UsingIndex()) {
  264.     EntryKey e(this->name.c_str());
  265.     int rv = FindKey(e);
  266.     if(rv) {  // Found the object in the index file
  267.       FAU addr = e.object_address;
  268.       RemoveObject(e.object_address); // Remove from the data file
  269.       RemoveKey(e);                   // Remove from the index file
  270.       return addr;
  271.     }
  272.     else
  273.       return 0; // Could not remove
  274.   }
  275.  
  276.   FAU addr = Find();
  277.   if(!addr) return 0; // Object does not exist
  278.   RemoveObject(addr);
  279.   return addr;
  280. }
  281.  
  282. void Grocery::SaveChanges()
  283. // Will record any changes except for the object's variable
  284. // string members. All the other members have a fixed length
  285. // for which space has already has been allocated in the data
  286. // file. The objects address will remain the same in the data
  287. // file and the index file.
  288. {
  289.   ObjectHeader oh;
  290.   pod->OpenDatabase()->Seek(this->objectaddress);
  291.   ReadObjHeader(oh);
  292.   if(oh.ClassID != GetClassID()) return; // Incorrect object type
  293.  
  294.   ReadString(name);
  295.   ReadString(brand);
  296.   ReadString(store);
  297.  
  298.   Price pr = this->price;
  299.   Quantity qt = this->quantity;
  300.   Purchasing pur = this->purchasing;
  301.   LineTotal lt = this->line_total;
  302.  
  303.   pod->OpenDatabase()->Write(&pr, sizeof(Price));
  304.   pod->OpenDatabase()->Write(&qt, sizeof(Quantity));
  305.   pod->OpenDatabase()->Write(&pur, sizeof(Purchasing));
  306.   pod->OpenDatabase()->Write(<, sizeof(LineTotal));
  307. }
  308.  
  309. int Grocery::CompareIndex()
  310. // Compares the data file to the index file.
  311. // Returns true if data and index file match.
  312. {
  313.   if(!UsingIndex()) return 0;
  314.  
  315.   Grocery grocery(pod);
  316.   EntryKey key;
  317.   
  318.   FAU oa;          // Object Address
  319.   VBHeader vb;     // Variable Block Header
  320.   ObjectHeader oh; // Object Header
  321.   
  322.   int objects = 0; // Keeps track of good variable blocks
  323.   int matches = 0; // Keep track of matches
  324.   
  325.   FAU vbdfileEOF = pod->OpenDatabase()->GetEOF();
  326.   FAU addr = 0;
  327.   addr = pod->OpenDatabase()->FindFirstVB(addr); // Search the entire file
  328.  
  329.   if(addr == 0) { // No variable blocks found in file
  330. #ifdef CPP_EXCEPTIONS
  331.     throw CNoObjectsExist();
  332. #else
  333.     Error->SignalException(EHandler::NoObjectsExist, EHandler::DISPLAY);
  334.     return 0;
  335. #endif
  336.   }
  337.   
  338.   while(1) { 
  339.     if(addr >= vbdfileEOF) break;
  340.     pod->OpenDatabase()->Read(&vb, sizeof(VBHeader), addr);
  341.     if(vb.CkWord == CheckWord) {
  342.       if((__SBYTE__)vb.Status == NormalVB) {
  343.     oa = addr + sizeof(VBHeader);
  344.     ReadObjHeader(oh, oa);
  345.     if(oh.ClassID == GetClassID()) { 
  346.       objects++; // Increment the object count
  347.       grocery.Read(oa);
  348.       key.SetStrKey(grocery.GetName());
  349.       key.SetOA(oa);
  350.       key.SetCID(oh.ClassID);
  351.       int rv = FindKey(key, 1);
  352.       if(rv) matches++; // Index and data file match
  353.     }
  354.       }
  355.       addr = addr + vb.Length; // Goto the next variable block
  356.     }
  357.     else {
  358.       addr = pod->OpenDatabase()->VBSearch(addr); 
  359.       if(!addr) break;
  360.     }
  361.   }
  362.  
  363.   return objects == matches;
  364. }
  365.  
  366. int Grocery::RebuildIndexFile(const char *fname)
  367. {
  368.   if(!UsingIndex()) return 0;
  369.  
  370.   int rv; // (R)eturn (V)alue
  371.   int CacheSize = 15;
  372.  
  373.   VBDFilePtr f(new VBDFile);
  374.   Btree btx(CacheSize);
  375.   f->Create(fname, sizeof(BtreeHeader));
  376.   btx.Connect(f, 1);
  377.   
  378.   Grocery grocery(pod);
  379.   EntryKey key;
  380.   
  381.   FAU oa;          // Object Address
  382.   VBHeader vb;     // Variable Block Header
  383.   ObjectHeader oh; // Object Header
  384.   
  385.   int objects = 0; // Keeps track of good variable blocks
  386.   int inserts = 0; // Keep track of inserts
  387.   
  388.   FAU vbdfileEOF = pod->OpenDatabase()->GetEOF();
  389.   FAU addr = 0;
  390.   addr = pod->OpenDatabase()->FindFirstVB(addr); // Search the entire file
  391.  
  392.   if(addr == 0) { // No variable blocks found in file
  393. #ifdef CPP_EXCEPTIONS
  394.     throw CNoObjectsExist();
  395. #else
  396.     Error->SignalException(EHandler::NoObjectsExist, EHandler::DISPLAY);
  397.     return 0;
  398. #endif
  399.   }
  400.   
  401.   while(1) { 
  402.     if(addr >= vbdfileEOF) break;
  403.     pod->OpenDatabase()->Read(&vb, sizeof(VBHeader), addr);
  404.     if(vb.CkWord == CheckWord) {
  405.       if((__SBYTE__)vb.Status == NormalVB) {
  406.     oa = addr + sizeof(VBHeader);
  407.     ReadObjHeader(oh, oa);
  408.     if(oh.ClassID == GetClassID()) { 
  409.       objects++; // Increment the object count
  410.       grocery.Read(oa);
  411.       key.SetStrKey(grocery.GetName());
  412.       key.SetOA(oa);
  413.       key.SetCID(oh.ClassID);
  414.       rv = btx.Add(key);
  415.       if(!rv) { 
  416. #ifdef CPP_EXCEPTIONS
  417.         throw CAssertError();
  418. #else
  419.       Error->SignalException(EHandler::AssertError);
  420. #endif
  421.       }
  422.       else
  423.         inserts++; // Index and data file match
  424.     }
  425.       }
  426.       addr = addr + vb.Length; // Goto the next variable block
  427.     }
  428.     else {
  429.       addr = pod->OpenDatabase()->VBSearch(addr); 
  430.       if(!addr) break;
  431.     }
  432.   }
  433.  
  434.   return objects == inserts;
  435. }
  436. // ----------------------------------------------------------- //
  437. // ------------------------------- //
  438. // --------- End of File --------- //
  439. // ------------------------------- //
  440.